package maze.view;

import maze.app.Maze;
import maze.model.Cell;
import maze.model.Map;
import maze.model.type.Face;

import org.lwjgl.opengl.GL11;

public class CellSprite extends Sprite
{
	public CellSprite()
	{
		super("cell");
	}

	@Override
	public void init()
	{
	}

	@Override
	public void render()
	{
		GL11.glPushMatrix();

		// leave the blank for each side
		GL11.glTranslatef(Maze.BLANK_UNIT, Maze.BLANK_UNIT, 0.0f);

		Map map = Map.getInstance();

		for (int i = 0; i < map.size.x; i++)
		{
			for (int j = 0; j < map.size.y; j++)
			{
				Cell cell = map.cells[i][j];

				// draw walls
				if (!cell.canWallPass(Face.SOUTH))
				{
					GL11.glPushMatrix();
					GL11.glTranslatef((i + 0.5f) * Maze.UNIT, j * Maze.UNIT, 0.0f);
					GL11.glRotatef(90, 0, 0, 1);
					drawTexture();
					GL11.glPopMatrix();
				}
				if (!cell.canWallPass(Face.NORTH))
				{
					GL11.glPushMatrix();
					GL11.glTranslatef((i + 0.5f) * Maze.UNIT, (j + 1) * Maze.UNIT, 0.0f);
					GL11.glRotatef(90, 0, 0, 1);
					drawTexture();
					GL11.glPopMatrix();
				}
				if (!cell.canWallPass(Face.WEST))
				{
					GL11.glPushMatrix();
					GL11.glTranslatef(i * Maze.UNIT, (j + 0.5f) * Maze.UNIT, 0.0f);
					drawTexture();
					GL11.glPopMatrix();
				}
				if (!cell.canWallPass(Face.EAST))
				{
					GL11.glPushMatrix();
					GL11.glTranslatef((i + 1.0f) * Maze.UNIT, (j + 0.5f) * Maze.UNIT, 0.0f);
					drawTexture();
					GL11.glPopMatrix();
				}
			}
		}

		GL11.glPopMatrix();
	}
}
